package com.ay.weixin.fdc.boss.kanfang.db;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lizar.db.ibatis.Dao;

import com.ay.weixin.config.types.KanFangProgress;
import com.ay.weixin.config.types.KanFangStatus;
import com.ay.weixin.fdc.boss.kanfang.domain.KanFang;
import com.ay.weixin.fdc.boss.kanfang.domain.KanFangDetail;

public class IbatisKanFangDao extends Dao{

	public void insert(KanFang kanfang) throws SQLException{
		this.sqlMapper.insert("fdc_kanfang.insert",kanfang);
	}
	
	
	
	public void batch_insert(List<KanFang> kanfang_list) throws SQLException{
		this.sqlMapper.startBatch();
		
		for(KanFang kanfang:kanfang_list){
			this.sqlMapper.insert("fdc_kanfang.insert",kanfang);
		}
		
		this.sqlMapper.executeBatch();
		
	}
	
	
	public void update(Map kanfang) throws SQLException{
		this.sqlMapper.update("fdc_kanfang.update",kanfang);
	}
	
	public void update(KanFang kanfang) throws SQLException{
		this.sqlMapper.update("fdc_kanfang.update_kanfang",kanfang);
	}
	
	public void update_on_visit_paid(long id,float visit_paid) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("id", id);
		keys.put("visit_paid", visit_paid);
		this.sqlMapper.update("fdc_kanfang.update_on_visit_paid",keys);
	}
	
	public void update_recommend_paid(long id,float commission) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("id", id);
		keys.put("commission", commission);
		this.sqlMapper.update("fdc_kanfang.update_recommend_paid",keys);
	}
	
	public KanFang get(long id) throws SQLException{
		return (KanFang)this.sqlMapper.queryForObject("fdc_kanfang.get",id);
	}
	
	public int getRecommandNumber(long user_id) throws SQLException{
		Integer i=(Integer)this.sqlMapper.queryForObject("fdc_kanfang.getRecommandNumber",user_id);
		if(i==null)return 0;
		return i;
	}
	
	public List<KanFangDetail> getByWhereCondition(long wxproject_id,String where) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("where", where);
		keys.put("wxproject_id", wxproject_id);
		return (List<KanFangDetail>)this.sqlMapper.queryForList("fdc_kanfang.getByWhereCondition",keys);
	}
	
	public int countByWhereCondition(long wxproject_id,String where) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("where", where);
		keys.put("wxproject_id", wxproject_id);
		Integer result=(Integer)this.sqlMapper.queryForObject("fdc_kanfang.countByWhereCondition",keys);
		if(result==null)return 0;
		return result;
	}
	
	public int countSearchByWhereCondition(long wxproject_id,String where,String name) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("where", where);
		keys.put("name", name);
		keys.put("wxproject_id", wxproject_id);
		Integer result=(Integer)this.sqlMapper.queryForObject("fdc_kanfang.countSearchByWhereCondition",keys);
		if(result==null)return 0;
		return result;
	}
	
	public int countByWhereConditionAndContact(long wxproject_id,String where,String contact) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("where", where);
		keys.put("contact", contact);
		keys.put("wxproject_id", wxproject_id);
		Integer result=(Integer)this.sqlMapper.queryForObject("fdc_kanfang.countByWhereConditionAndContact",keys);
		if(result==null)return 0;
		return result;
	}
	
	
	
	public List<KanFang> getByWhereConditions(long wxproject_id,String where,int start,int size) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("where", where);
		keys.put("start", start);
		keys.put("size", size);
		keys.put("wxproject_id", wxproject_id);
		return (List<KanFang>)this.sqlMapper.queryForList("fdc_kanfang.getByWhereConditions",keys);
	}
	
	public List<KanFang> searchByWhereCondition(long wxproject_id,String where,String name,int start,int size) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("where", where);
		keys.put("name", name);
		keys.put("start", start);
		keys.put("size", size);
		keys.put("wxproject_id", wxproject_id);
		return (List<KanFang>)this.sqlMapper.queryForList("fdc_kanfang.searchByWhereCondition",keys);
	}
	
	public List<KanFangDetail> searchAllByWhereCondition(long wxproject_id,String where,String name) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("where", where);
		keys.put("name", name);
		keys.put("wxproject_id", wxproject_id);
		return (List<KanFangDetail>)this.sqlMapper.queryForList("fdc_kanfang.searchAllByWhereCondition",keys);
	}
	
	public List<KanFang> searchByWhereConditionAndContact(long wxproject_id,String where,String contact,int start,int size) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("where", where);
		keys.put("contact", contact);
		keys.put("start", start);
		keys.put("size", size);
		keys.put("wxproject_id", wxproject_id);
		return (List<KanFang>)this.sqlMapper.queryForList("fdc_kanfang.searchByWhereConditionAndContact",keys);
	}
	
	public List<KanFangDetail> searchAllByWhereConditionAndContact(long wxproject_id,String where,String contact) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("where", where);
		keys.put("contact", contact);
		keys.put("wxproject_id", wxproject_id);
		return (List<KanFangDetail>)this.sqlMapper.queryForList("fdc_kanfang.searchAllByWhereConditionAndContact",keys);
	}
	
	public List<KanFang> getAllReadyReturn(long wxproject_id) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("progress_closed", KanFangProgress.CLOSED.getValue());
		keys.put("status_success", KanFangStatus.sign_success.getValue());
		keys.put("wxproject_id", wxproject_id);
		return (List<KanFang>)this.sqlMapper.queryForList("fdc_kanfang.getAllReadyReturn",keys);
	}
	
	public List<KanFang> getAllINProgress(long wxproject_id) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("progress_closed", KanFangProgress.CLOSED.getValue());
		keys.put("wxproject_id", wxproject_id);
		return (List<KanFang>)this.sqlMapper.queryForList("fdc_kanfang.getAllINProgress",keys);
	}
	
	public List<KanFang> getSalesList(long user_id) throws SQLException{
		return (List<KanFang>)this.sqlMapper.queryForList("fdc_kanfang.getSalesList",user_id);
	}
	
	
	public List<KanFang> getRecommandList(long user_id) throws SQLException{
		return (List<KanFang>)this.sqlMapper.queryForList("fdc_kanfang.getRecommandList",user_id);
	}
	
	public List<KanFangDetail> getRecommandDetailList(long user_id) throws SQLException{
		return (List<KanFangDetail>)this.sqlMapper.queryForList("fdc_kanfang.getRecommandDetailList",user_id);
	}
	
	public List<KanFangDetail> getSalesDetailList(long user_id) throws SQLException{
		return (List<KanFangDetail>)this.sqlMapper.queryForList("fdc_kanfang.getSalesDetailList",user_id);
	}
	
	public KanFang getLastOngoingBook(long wxproject_id, String contact) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("contact", contact);
		keys.put("progress_closed", KanFangProgress.CLOSED.getValue());
		keys.put("wxproject_id", wxproject_id);
		return (KanFang)this.sqlMapper.queryForObject("fdc_kanfang.getLastOngoingBook",keys);
	}
	
	public boolean hasLogged(long wxproject_id,String contact,long project_id) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("contact", contact);
		keys.put("project_id",project_id);
		keys.put("wxproject_id", wxproject_id);
		return this.sqlMapper.queryForObject("fdc_kanfang.hasLogged",keys)!=null;
	}
	
	public KanFang getLastBook(long wxproject_id,String contact) throws SQLException{
		Map<String,Object> keys=new HashMap<String,Object>();
		keys.put("contact", contact);
		keys.put("wxproject_id", wxproject_id);
		return (KanFang)this.sqlMapper.queryForObject("fdc_kanfang.getLastBook",keys);
	}
	
	public boolean exists(long id) throws SQLException{
		Object obj=this.sqlMapper.queryForObject("fdc_kanfang.exists",id);
		if(obj==null)return false;
		return true;
	}
	
	
}
